import enum

import pygame


class ActionByMulti:
    def __init__(self, path_expression: str, image_count: int, is_loop: bool):
        """
        初始化
        :param path_expression: 图片路径
        :param image_count: 图片数量
        :param is_loop: 是否循环
        """
        self.image_index = 0
        self.action_images = []
        self.image_count = image_count
        self.is_loop = is_loop

        for i in range(0, image_count):
            img_path = str.format(path_expression % (i + 1))
            self.action_images.append(pygame.image.load(img_path))

    def get_image(self) -> pygame.Surface:
        """
        获取当前显示图片
        :return:
        """
        current_img = self.action_images[self.image_index]
        if self.image_index + 1 >= self.image_count:
            if self.is_loop:
                self.image_index = 0
        else:
            self.image_index += 1
        return current_img

    def is_end(self) -> bool:
        """
        图片是否绘制结束
        :return: True
        """
        if self.is_loop:
            return False
        else:
            if self.image_index >= self.image_count - 1:
                return True
            else:
                return False


class WalkDir(enum.IntEnum):
    down = 0
    left = 1
    right = 2
    up = 3


class DirWalkByAImage:
    """
    一张图 4*4人物 下 左 右 上
    """
    image_count = 16
    image_col = 4
    image_width = 49
    image_height = 74
    walk_dir = WalkDir.down

    def __init__(self, path_name: str, image_width: int, image_height: int, is_loop: bool):
        """
        初始化
        :param path_name: 图片路径
        :param image_width: 单张图片宽度
        :param image_height: 单张图片高度
        :param is_loop: 是否循环
        """
        self.path_name = path_name
        self.image_index = 0
        self.action_images = []  # 四个方向的图片数组
        self.image_height = image_height
        self.image_width = image_width

        self.is_loop = is_loop
        print(path_name)
        self.image = pygame.image.load(path_name)  # .convert()
        for i in range(0, self.image_count):
            row = int(i / self.image_col)
            col = i % self.image_col
            # print(row, col)
            if col == 0:
                dir = []
                self.action_images.append(dir)

            rect = (col * self.image_width,
                    row * self.image_height,
                    self.image_width,
                    self.image_height)
            # print(rect)
            image = self.image.subsurface(rect)
            dir.append(image)

    def get_image(self) -> pygame.Surface:
        """
        获取当前显示图片
        :return:
        """
        current_image = self.action_images[self.walk_dir][self.image_index]
        self.image_index += 1
        if self.image_index >= self.image_col:
            if self.is_loop:
                self.image_index = 0
            else:
                self.image_index = self.image_col - 1
        return current_image

    def set_dir(self, dir: WalkDir):
        """
        设置方向
        :param dir:  方向
        :return:
        """
        self.walk_dir = dir

    def is_end(self) -> bool:
        """
        是否播放完毕
        :return:
        """
        if self.is_loop:
            return False
        else:
            if self.image_index >= self.image_col - 1:
                return True
            else:
                return False


class ShootDir(enum.IntEnum):
    not_shoot = 0
    left_down = 1
    right_down = 2
    left_up = 3
    right_up = 4
    up = 5
    down = 6


class DirShootByAImage(DirWalkByAImage):
    """
    一张图 4*4人物  左蹲射击， 右蹲射击，左站射击，右站射击
    """
    image_count = 16
    image_col = 4
    image_width = 49
    image_height = 74
    shoot_dir = ShootDir.left_up

    @staticmethod
    def __init_shoot(up_image_path, down_image_path):
        """
        初始化射击图片
        :param up_image_path:
        :param down_image_path:
        :return:
        """

        DirShootByAImage.up_shoot_image = pygame.image.load(up_image_path).convert_alpha()
        DirShootByAImage.down_shoot_image = pygame.image.load(down_image_path).convert_alpha()

    def __init__(self, path_name: str, image_width: int, image_height: int, is_loop: bool):
        super(DirShootByAImage, self).__init__(path_name, image_width, image_height, is_loop)
        self.__init_shoot("./resources/images/shoot/up_shoot.png",
                          "./resources/images/shoot/down_shoot.png")

    def get_image(self) -> pygame.Surface:
        """
        获取射击图片
        :return:
        """
        if self.shoot_dir == ShootDir.not_shoot:
            return None
        if self.shoot_dir < ShootDir.up:
            current_img = self.action_images[self.shoot_dir - 1][self.image_index]
            self.image_index += 1
            if self.image_index >= self.image_col:
                if self.is_loop:
                    self.image_index = 0
                else:
                    self.image_index = self.image_col - 1
        elif self.shoot_dir == ShootDir.up:
            current_img = DirShootByAImage.up_shoot_image
        else:
            current_img = DirShootByAImage.down_shoot_image
        return current_img

    def set_dir(self, dir: ShootDir):
        """
        获取射击方向
        :param dir:
        :return:
        """
        self.shoot_dir = dir


def red_army_soldier():
    return None